package org.sportspace.system.utils

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.core.io.ClassPathResource

class SportSpaceInit {
    static Logger logger = LoggerFactory.getLogger(SportSpaceInit.class);
    //Top-Config.properties初始化配置文件
    private final static String CFG_DIR = "${System.getProperty("user.home")}/.sportspace";
    private static String sportSpaceTemplate = "SportSpace-config.properties";
    private static String CFG_SPORTSPACE_FILE_PATH = "${CFG_DIR}/${sportSpaceTemplate}";
    //拷贝DLL

    public static void initSystem() {
        this.loadCongifFile()
    }

    /**
     * 拷贝SportSpace-config.properties to .sportspace.base.domain
     */
    private static void loadCongifFile() {
        File tempFile = null;
        //TopCA-config.properties
        File cfgTopFile = new File(CFG_SPORTSPACE_FILE_PATH);
        if (!cfgTopFile.exists()) {
            try {
                tempFile = new File(new ClassPathResource(sportSpaceTemplate).file.absolutePath);
            } catch (Exception e) {
                tempFile = new File("./grails-app/conf/${sportSpaceTemplate}");
            }
            new AntBuilder().copy tofile: CFG_SPORTSPACE_FILE_PATH, file: tempFile;
        }

        def props = new Properties();
        try {
            cfgTopFile.withInputStream {instream ->
                props.load(instream)
            }
        } catch (IOException ex) {
            throw new NullPointerException("load the [${cfgTopFile.toURL()}] failed...")
        }
        ConfigHolder.config = new ConfigSlurper().parse(props);
        System.setProperty("appCfgDir",CFG_DIR);
    }
}
